//
//  PopUpViewController.m
//  reacticons
//
//  Created by Admin on 7/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PopUpViewController.h"
#import <QuartzCore/QuartzCore.h>

#define POPUP_BAR_WIDTH     232
#define POPUP_BAR_HEIGHT    67

@implementation PopUpViewController

@synthesize addToFavoriteButton;
@synthesize delegate;
@synthesize selectedFrame;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)setSelectedFrame:(CGRect)newSelectedFrame
{
    selectedFrame = newSelectedFrame;
    CGRect superViewFrame = self.view.superview.bounds;
    
    DBLOG(@"%f, %f", selectedFrame.origin.x, selectedFrame.origin.y);
    
    if (selectedFrame.origin.y > (superViewFrame.origin.y + selectedFrame.size.height)) {
        [topCopiedBtn setHidden:NO];
        [botCopiedBtn setHidden:YES];
        [topCopiedBtn performSelector:@selector(setHidden:) withObject:[NSNumber numberWithBool:YES] afterDelay:2.0];
    }else{
        [topCopiedBtn setHidden:YES];
        [botCopiedBtn setHidden:NO];
        [botCopiedBtn performSelector:@selector(setHidden:) withObject:[NSNumber numberWithBool:YES] afterDelay:2.0];
    }
    
    if (selectedFrame.origin.x > (superViewFrame.size.width/2))
    {
        if (selectedFrame.origin.y < (superViewFrame.origin.y + selectedFrame.size.height)) {
            [self setPositionOfArrow:PopUpArrowAtTopRight];
            [self.view setFrame:CGRectMake(selectedFrame.origin.x + selectedFrame.size.width/2 - POPUP_BAR_WIDTH + 30, 
                                           selectedFrame.origin.y + selectedFrame.size.height - 12,
                                           POPUP_BAR_WIDTH, 
                                           POPUP_BAR_HEIGHT)];
        }
        else
        {
            [self setPositionOfArrow:PopUpArrowAtBottomRight];
            [self.view setFrame:CGRectMake(selectedFrame.origin.x + selectedFrame.size.width/2 - POPUP_BAR_WIDTH + 30, 
                                           selectedFrame.origin.y - POPUP_BAR_HEIGHT + 24,
                                           POPUP_BAR_WIDTH, 
                                           POPUP_BAR_HEIGHT)];
        }
    }
    else if (selectedFrame.origin.x +POPUP_BAR_WIDTH >320) {
        if (selectedFrame.origin.y < (superViewFrame.origin.y + selectedFrame.size.height)) {
            [self setPositionOfArrow:PopUpArrowAtTopCenter];
            [self.view setFrame:CGRectMake(selectedFrame.origin.x + selectedFrame.size.width/2 - POPUP_BAR_WIDTH/2, 
                                           selectedFrame.origin.y + selectedFrame.size.height - 12,
                                           POPUP_BAR_WIDTH, 
                                           POPUP_BAR_HEIGHT)];
        }
        else
        {
            [self setPositionOfArrow:PopUpArrowAtBottomCenter];
            [self.view setFrame:CGRectMake(selectedFrame.origin.x + selectedFrame.size.width/2 - POPUP_BAR_WIDTH/2, 
                                           selectedFrame.origin.y - POPUP_BAR_HEIGHT + 24,
                                           POPUP_BAR_WIDTH, 
                                           POPUP_BAR_HEIGHT)];
        }
    }
    
    else if (selectedFrame.origin.x < (superViewFrame.size.width/2)) 
    {
        if (selectedFrame.origin.y < (superViewFrame.origin.y + selectedFrame.size.height)) {
            [self setPositionOfArrow:PopUpArrowAtTopLeft];
            [self.view setFrame:CGRectMake(selectedFrame.origin.x + selectedFrame.size.width/2 - 30, 
                                           selectedFrame.origin.y + selectedFrame.size.height - 12,
                                           POPUP_BAR_WIDTH, 
                                           POPUP_BAR_HEIGHT)];
        }
        else
        {
            [self setPositionOfArrow:PopUpArrowAtBottomLeft];
            [self.view setFrame:CGRectMake(selectedFrame.origin.x + selectedFrame.size.width/2 - 40,
                                           selectedFrame.origin.y - POPUP_BAR_HEIGHT + 24,
                                           POPUP_BAR_WIDTH, 
                                           POPUP_BAR_HEIGHT)];
        }
    }
    
}

- (void)setPositionOfArrow:(PopUpArrowPosition)position
{
    switch (position) {
        case PopUpArrowAtTopLeft:
            [arrow1 setHidden:NO];
            [arrow2 setHidden:YES];
            [arrow3 setHidden:YES];
            [arrow4 setHidden:YES];
            [arrowcenter1 setHidden:YES];
            [arrowcenter2 setHidden:YES];
            break;
            
        case PopUpArrowAtTopRight:
            [arrow1 setHidden:YES];
            [arrow2 setHidden:NO];
            [arrow3 setHidden:YES];
            [arrow4 setHidden:YES];
            [arrowcenter1 setHidden:YES];
            [arrowcenter2 setHidden:YES];
            break;
            
        case PopUpArrowAtBottomLeft:
            [arrow1 setHidden:YES];
            [arrow2 setHidden:YES];
            [arrow3 setHidden:NO];
            [arrow4 setHidden:YES];
            [arrowcenter1 setHidden:YES];
            [arrowcenter2 setHidden:YES];
            break;
            
        case PopUpArrowAtBottomRight:
            [arrow1 setHidden:YES];
            [arrow2 setHidden:YES];
            [arrow3 setHidden:YES];
            [arrow4 setHidden:NO];
            [arrowcenter1 setHidden:YES];
            [arrowcenter2 setHidden:YES];
            break;
            
        case PopUpArrowAtTopCenter:
            [arrow1 setHidden:YES];
            [arrow2 setHidden:YES];
            [arrow3 setHidden:YES];
            [arrow4 setHidden:YES];
            [arrowcenter1 setHidden:NO];
            [arrowcenter2 setHidden:YES];
            break;
        case PopUpArrowAtBottomCenter:
            [arrow1 setHidden:YES];
            [arrow2 setHidden:YES];
            [arrow3 setHidden:YES];
            [arrow4 setHidden:YES];
            [arrowcenter1 setHidden:YES];
            [arrowcenter2 setHidden:NO];
            break;
        default:
            [arrow1 setHidden:YES];
            [arrow2 setHidden:YES];
            [arrow3 setHidden:YES];
            [arrow4 setHidden:YES];
            [arrowcenter1 setHidden:YES];
            [arrowcenter2 setHidden:YES];
            break;
    }
}

#pragma mark - Buttons On Clicked

- (IBAction)buttonsOnClicked:(id)sender
{
    DBLOG(@"%d",[sender tag]);
    [self.delegate popUpBar:self didSelectButton:sender];
}

- (void)setStatusOfFavoriteButtonAsActive:(BOOL)active
{
    if (active) {
        [addToFavoriteButton setImage:[UIImage imageNamed:@"filled_star.png"] forState:UIControlStateNormal];
    }
    else
        [addToFavoriteButton setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
}

@end
